﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class FoodService : BaseService<Food>, IFoodService
    {
        public FoodService() : base(RepositoryFactory.FoodRepository) { }

        public IQueryable<Food> FindList(int number, string title, int restaurant_Id, int category_Id, int chef_Id, DateTime? fromDate, DateTime? toDate, int orderCode)
        {
            return FindPageList(out number, 1, number, title,restaurant_Id, category_Id, chef_Id, fromDate, toDate, orderCode);
        }

        public IQueryable<Food> FindPageList(out int totalRecord, int pageIndex, int pageSize, string title, int restaurant_Id, int category_Id, int chef_Id, DateTime? fromDate, DateTime? toDate, int orderCode)
        {
            //获取实体列表
            IQueryable<Food> _commonModels = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(title)) _commonModels = _commonModels.Where(cm => cm.Name.Contains(title));
            if (restaurant_Id > 0) _commonModels = _commonModels.Where(food => food.Restaurant_Id == restaurant_Id);
            if (category_Id > 0) _commonModels = _commonModels.Where(food => food.Category_Id == category_Id);
            if (chef_Id > 0) _commonModels = _commonModels.Where(cm => cm.Chef_Id == chef_Id);
            
            if (fromDate != null) _commonModels = _commonModels.Where(cm => cm.CreateTime >= fromDate);
            if (toDate != null) _commonModels = _commonModels.Where(cm => cm.CreateTime <= toDate);

            _commonModels = Order(_commonModels, orderCode);
            totalRecord = _commonModels.Count();
            return PageList(_commonModels, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<Food> Order(IQueryable<Food> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.CreateTime);
                    break;
            }
            return entitys;
        }
    }
}
